package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSectionExpertProgressService;
import com.bidevalution.business.service.BeSectionMethodService;
import com.bidevalution.business.service.BeSectionReappraisalRecordService;
import com.bidevalution.business.service.BeSectionStepJobService;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.*;
import com.bidevalution.dao.mapper.*;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.exception.BizException;
import com.google.common.collect.Maps;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @description: 评标标段重评记录接口实现类
 * @author: xiayf
 * @date: 2019-10-08 13:20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BeSectionReappraisalRecordServiceImpl implements BeSectionReappraisalRecordService {
	@Autowired
	private BeSectionReappraisalRecordMapper beSectionReappraisalRecordMapper;
	@Autowired
	private BeEvalutionOutcomeMapper beEvalutionOutcomeMapper;
	@Autowired
	private BeSectionStepMapper beSectionStepMapper;
	@Autowired
	private BeSectionExpertProgressMapper beSectionExpertProgressMapper;
	@Autowired
	private BeEvalutionSummaryMapper beEvalutionSummaryMapper;
	@Autowired
	private BeSectionStepJobMapper beSectionStepJobMapper;
	@Autowired
	private BeSectionMethodMapper beSectionMethodMapper;
	@Autowired
	private BeSectionExpertProgressService beSectionExpertProgressService;

	@Override
	@Transactional
	public void saveReappraisalRecord(BeSectionStep beSectionStep, String submitCode, String submitType, String userId)
			throws Exception {
		BeSectionReappraisalRecord record = new BeSectionReappraisalRecord();
		record.setSeqId(IdsUtil.uuid());
		record.setSectionId(beSectionStep.getSectionId());
		record.setSectionStepCode(beSectionStep.getStepCode());
		record.setSectionStepId(beSectionStep.getSeqId());
		record.setCreateUserId(userId);
		record.setSubmitCode(submitCode);
		record.setCreateTime(DateUtil.now());
		record.setSubmitType(submitType);
		record.setIsSuccess(Constants.FALSE);
		if (beSectionReappraisalRecordMapper.insert(record) < 1) {
			throw new Exception("*****保存重评记录失败！");
		}
	}

	@Override
	public BeSectionReappraisalRecord queryRecordByObject(BeSectionReappraisalRecord record) throws Exception {
		return beSectionReappraisalRecordMapper.queryRecordByObject(record);
	}

	@Override
	@Transactional
	public void updateRecordByObject(String sectionId, String submitCode, String submitType, String sectionStepId) throws Exception {
		BeSectionReappraisalRecord record = new BeSectionReappraisalRecord();
		record.setSectionId(sectionId);
		record.setSubmitCode(submitCode);
		record.setSubmitType(submitType);
		record.setIsSuccess(Constants.TRUE);
		if (beSectionReappraisalRecordMapper.updateRecordByObject(record) < Constants.ONE) {
			throw new Exception("更新替补或重评成功状态失败！");
		}
	}
	@Override
	@Transactional
	public void updateRecordByObject(String sectionId, String submitCode, String submitType, BeSectionStep currStep,BeSectionStep preStep) throws Exception {
		BeSectionReappraisalRecord record = new BeSectionReappraisalRecord();
		record.setSectionId(sectionId);
		record.setSubmitCode(submitCode);
		record.setSubmitType(submitType);
		record = beSectionReappraisalRecordMapper.queryRecordByObject(record);
		record.setIsSuccess(Constants.TRUE);
		if("1".equals(submitType)) {
			//重新生成提交版本号,重置替补的两个环节的所有数据
	    	String newSubmitCode = IdsUtil.uuid();
	    	record.setSubmitCode(newSubmitCode);
	        beSectionExpertProgressService.updateStatusByIdAlt(sectionId, currStep.getStepCode(), "0", newSubmitCode,"2");
	        beSectionExpertProgressService.updateStatusByIdAlt(sectionId, preStep.getStepCode(), "0", newSubmitCode,"2");
	        beSectionReappraisalRecordMapper.updateByPrimaryKey(record);
	        BeSectionMethod beSectionMethod = beSectionMethodMapper.selectByPrimaryKey(sectionId);
	        beSectionMethod.setSubmitCode(newSubmitCode);
	        beSectionMethodMapper.updateByPrimaryKey(beSectionMethod);
		}else {
			if (beSectionReappraisalRecordMapper.updateRecordByObject(record) < Constants.ONE) {
				throw new Exception("更新替补或重评成功状态失败！");
			}
		}
		
		
		
	}

	@Override
	public int deleteRecordByObject(BeSectionReappraisalRecord record) {
		return beSectionReappraisalRecordMapper.deleteRecordByObject(record);
	}
}
